<template>
	<div class="system-role-container">
		<el-dialog
			title="添加角色"
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			destroy-on-close
			v-model="isShowDialog"
			:show-close="false"
			width="369px"
		>
			<el-form :model="ruleForm" :rules="rules" ref="roleform" size="small" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色名称" prop="roleName">
							<el-input v-model="ruleForm.roleName" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="权限树" prop="permissions">
							<el-input clearable v-model="filterText" placeholder="请输入关键字" />
							<el-tree
								style="margin-top:15px"
								ref="treeMenu"
								:filter-node-method="filterNode"
								:props="propstree"
								:data="treeList"
								show-checkbox
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, watch, ref, defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import { getMenuTree } from '/@/api/platform/menu/index'
import { add } from '/@/api/platform/role/index'
export default defineComponent({
	name: 'AddRole',
	setup(props: any, context: any) {
		const state = reactive({
			isShowDialog: false,
			ruleForm: {
				roleName: "", // 角色名称
				permissions: [{}], // 角色权限树
			},
			rules: {
				roleName: [{
					required: true,
					message: "请输入角色名称！",
					trigger: "blur"
				}],
				permissions: [{
					required: true,
					validator: (rule: any, value: any, callback: any) => {
						//获取全选节点
						const checkMenu = treeMenu.value.getCheckedNodes();
						//获取半选节点
						const halfCheckMenu = treeMenu.value.getHalfCheckedNodes()
						state.ruleForm.permissions = [];
						if (checkMenu && checkMenu.length > 0) {
							checkMenu.forEach((menu: any) => {
								state.ruleForm.permissions.push(
									{
										"permissionName": menu.MenuName,
										"permission": menu.Permission
									})
							});
							if (halfCheckMenu && halfCheckMenu.length > 0) {
								halfCheckMenu.forEach((menu: any) => {
									state.ruleForm.permissions.push({
										"permissionName": menu.MenuName,
										"permission": menu.Permission
									})
								});
							}
							callback()
						} else {
							callback(new Error('请选择角色授予的权限！'))
						}
					},
					trigger: "blur"
				}]
			},
			propstree: {
				label: 'MenuName',
				children: 'Children',
			},
			treeList: [],
			filterText: ""
		});
		const roleform = ref();
		const treeMenu = ref();
		watch(() => state.filterText, (value) => {
			treeMenu.value.filter(value);
		})
		const filterNode = (value: any, data: any) => {
			if (!value) return true
			return data.MenuName.indexOf(value) !== -1
		}
		const initTableData = () => {
			getMenuTree({ parentID: 0 }).then((res) => {
				state.treeList = res.Extras;
			})
		};
		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
			initTableData();
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
			initForm();
		};
		const onSubmit = () => {
			roleform.value.validate((valid: boolean) => {
				if (!valid) {
					return false;
				}
				add(state.ruleForm).then((res) => {
					ElMessageBox.alert(res.Message, '提示', {}).then(() => {
						// 刷新父级数据
						context.emit("sendRefresh");
						closeDialog(); // 关闭弹窗
						initForm();
					}).catch(() => { });
				})
			})
		};
		// 表单初始化，方法：`resetFields()` 无法使用
		const initForm = () => {
			roleform.value.resetFields();
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
			roleform,
			treeMenu,
			filterNode,
		};
	},
});
</script>